// Author: Domien Nowicki

#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>

#include "MapPointItem.h"
#include "MapWidget.h"

MapPointItem::MapPointItem(MapWidget *mapView, int x, int y): map(mapView), Point(x, y)
{
	// Point items must be on top of everything
	setZValue(POINTZ);
	// Setup its position
	setPos(QPoint( GetX(), GetY()) );
}

MapPointItem::MapPointItem(const MapPointItem& rhs): map(rhs.map), Point(rhs.GetX(), rhs.GetY()) 
{
	// Point items must be on top of everything
	setZValue(POINTZ);
	setPos(QPoint( GetX(), GetY()) );
}

MapPointItem::MapPointItem(MapWidget *mapView, const Point& rhs): map(mapView), Point(rhs.GetX(), rhs.GetY())
{
	// Point items must be on top of everything
	setZValue(POINTZ);
	setPos(QPoint( GetX(), GetY()) );
}

MapPointItem::~MapPointItem()
{
}

QRectF MapPointItem::boundingRect() const
{
	// Return bounding rectangle of this point item
	qreal adjust = POINTADJUST;

	// Take into account a little adjustment
	return QRectF(-(NODEWIDTH/2) - adjust, -(NODEHEIGHT/2) - adjust, NODEWIDTH + adjust, NODEHEIGHT + adjust);
}

QPainterPath MapPointItem::shape() const
{
	QPainterPath path;
	// Return the shape of this item
	path.addEllipse(-(NODEWIDTH/2), -(NODEHEIGHT/2), NODEWIDTH, NODEHEIGHT);
	return path;
}

void MapPointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	// Paint this point item
	QRadialGradient radialGradient(0, 0, NODERADIUS, 0, 0);

	// Create a brownish radial gradient
	radialGradient.setColorAt(0.0, QColor(244,210,148));
	radialGradient.setColorAt(1.0, QColor(110,72,0));

	// Draw the ellipse which represents a point of the province
	painter->setBrush(radialGradient);
	painter->setPen(QPen(Qt::black, NODEPENWIDTH, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
	painter->drawEllipse(QRect(-(NODEWIDTH/2), -(NODEHEIGHT/2), NODEWIDTH, NODEHEIGHT));
}

void MapPointItem::AddToScene(QGraphicsScene *newScene, QGraphicsItem *item)
{
	// Add item to scene, or add self to scene if item is null
	if (newScene)
	{
		if (0 == item)
			item = this;

		// Add the item to the scene (most likely itself)
		newScene->addItem(item);
	}
}

